package netty.netty01.c2;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @author ZH
 * Date: 2022/6/8
 */
public class HelloClient {
    public static void main(String[] args) throws InterruptedException {
        new Bootstrap()
                //添加EventLoop
                .group(new NioEventLoopGroup())
                //添加channel
                .channel(NioSocketChannel.class)
                //添加处理器
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    //在链接建立后被调用
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringEncoder()); //String编码
                    }
                })
                .connect("127.0.0.1", 8080)
                .sync()
                .channel()
                //向服务器发送数据
                .writeAndFlush("helloworld");

    }
}
